package com.landiao.common.utils;

import java.security.MessageDigest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;

public class CodecUtils {
	
	private final static String PASSWORD_SALT = "jxy";
	
	public final static String ORDER_PRE = "XF";//订单消费
	
	public final static String RECHARGE_PRE = "CZ";//充值
	
	public final static String MD5(String str) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'A', 'B', 'C', 'D', 'E', 'F' };
		try {
			byte[] btInput = str.getBytes();
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			mdInst.update(btInput);
			byte[] md = mdInst.digest();
			int j = md.length;
			char strs[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				strs[k++] = hexDigits[byte0 >>> 4 & 0xf];
				strs[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(strs);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String password(String str) {
		if(StringUtils.isEmpty(str)){
			throw new IllegalArgumentException();
		}
		return MD5(PASSWORD_SALT+str);
	}
	
	/**
	 * 生成UUID
	 * @return
	 */
	public static String getUUID() {  
		String idstr = UUID.randomUUID().toString().replace("-", "");
		return idstr.toUpperCase();
	}
	/**
	 * 获取系统当前时间数字 050720161622
	 * @return
	 */
	public static String getDateNum(){
		String result = null;
		Date dt=new Date();//如果不需要格式,可直接用dt,dt就是当前系统时间
		DateFormat df = new SimpleDateFormat("yy/MM/dd HH:mm:ss");//设置显示格式
		String nowTime="";
		nowTime= df.format(dt);//用DateFormat的format()方法在dt中获取并以yyyy/MM/dd HH:mm:ss格式显示
		result = nowTime.replace("/", "").replace(":", "").replace(" ", "").trim();
		return result;
	}
	
	public static String getOrderNo(){
		StringBuilder sb = new StringBuilder();
		sb.append(ORDER_PRE);
		sb.append(DateUtil.date2Str(new Date(), DateUtil.DATETIMEFORMAT_STR));
		sb.append(RandomStringUtils.randomNumeric(4));
		return sb.toString();
	}
	
	public static String getRechargeNo(){
		StringBuilder sb = new StringBuilder();
		sb.append(RECHARGE_PRE);
		sb.append(DateUtil.date2Str(new Date(), DateUtil.DATETIMEFORMAT_STR));
		sb.append(RandomStringUtils.randomNumeric(4));
		return sb.toString();
	}
}
